package com.example.springbootsqlite.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springbootsqlite.mapper.UserMapper;
import com.example.springbootsqlite.model.User;
import com.example.springbootsqlite.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author cphovo
 * create datetime 2022/6/13 23:20
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource
    UserMapper userMapper;

    @Override
    public Integer save(User user) {
        return userMapper.insert(user);
    }

    @Override
    public Integer removeById(Integer id) {
        return userMapper.deleteById(id);
    }

    @Override
    public User getById(Integer id) {
        return userMapper.selectById(id);
    }

    @Override
    public List<User> list(Wrapper<User> queryWrapper) {
        List<User> userList = userMapper.selectList(queryWrapper);
        if (userList == null || userList.size() == 0) {
            return new ArrayList<>();
        }
        return userMapper.selectList(queryWrapper);
    }

    @Override
    public IPage<User> page(Integer current, Integer size) {
        Page<User> page = new Page<>(current, size);
        return userMapper.selectPage(page, new QueryWrapper<>());
    }

    @Override
    public Integer updateById(User user) {
        return userMapper.updateById(user);
    }
}
